using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.MeasureInfo.Input;
//using HuiLian.Service.Platform.MeasureInfo.Output;
using HuiLian.Common.Attributes;
using HuiLian.Common;

namespace HuiLian.Service.Platform.MeasureInfo
{
    /// <summary>
    /// 测量数据表服务
    /// </summary>
    public class MeasureInfoService : IMeasureInfoService
    {
        private readonly IMapper _mapper;
        private readonly IMeasureInfoRepository _measureInfoRepository;

        public MeasureInfoService(IMapper mapper, IMeasureInfoRepository measureInfoRepository)
        {
            _mapper = mapper;
            _measureInfoRepository = measureInfoRepository;
        }

        /// <summary>
        /// 获取测量数据表信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _measureInfoRepository.GetAsync(id);
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询测量数据表信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<MeasureInfoEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _measureInfoRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderBy(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync();

            var data = new PageOutput<MeasureInfoEntity>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 添加测量数据表信息
        /// </summary>
        /// <param name="input">测量数据表信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(MeasureInfoAddInput input)
        {
            var entity = _mapper.Map<MeasureInfoEntity>(input);
            var id = (await _measureInfoRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新测量数据表信息
        /// </summary>
        /// <param name="input">测量数据表信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(MeasureInfoUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _measureInfoRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _measureInfoRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除测量数据表信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _measureInfoRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除测量数据表信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _measureInfoRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软删除测量数据表信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _measureInfoRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 添加列表
        /// </summary>
        /// <param name="measureInfos"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddListAsync(List<MeasureInfoEntity> measureInfos)
        {
            var entitys = await _measureInfoRepository.Select.ToListAsync();
            List<MeasureInfoEntity> addList = new List<MeasureInfoEntity>();
            foreach (var measureInfo in measureInfos)
            {
                var entity = entitys.Where(p => p.漏点工单编号 == measureInfo.漏点工单编号).FirstOrDefault();
                if (entity == null) addList.Add(measureInfo);
            }

            if (addList.Count > 0)
            {
                var count = (await _measureInfoRepository.InsertAsync(addList)).Count;
                return ResponseOutput.Result(count > 0);
            }
            return ResponseOutput.Result(true);
        }

        public async Task<IResponseOutput> UpdatePaiDanTimeAndJieDanTimeFromExcel(List<MeasureInfoEntity> measureInfos)
        {
            var entitys = await _measureInfoRepository.Select.ToListAsync();
            foreach (var item in entitys)
            {
                var measureInfo = measureInfos.Where(p => p.漏点工单编号 == item.漏点工单编号).FirstOrDefault();
                if(measureInfo != null)
                {
                    item.派单时间 = measureInfo.派单时间;
                    item.接单时间 = measureInfo.接单时间;
                }
            }
            await _measureInfoRepository.UpdateAsync(entitys);
            return ResponseOutput.Result(true);
        }

    }
}
